using micromsg;
using MicroMsg.Common.Algorithm;
using MicroMsg.Common.Event;
using MicroMsg.Common.Utils;
using MicroMsg.Manager;
using MicroMsg.Protocol;
using MicroMsg.Scene;
using MicroMsg.Storage;
using System;
using System.Collections.Generic;
namespace MicroMsg.Plugin.Sns.Scene
{
	public class NetSceneSnsPost : NetSceneBaseEx<SnsPostRequest, SnsPostResponse, SnsPostRequest.Builder>
	{
		private const string TAG = "NetSceneSnsPost";
		public onSceneFinishedDelegate mOnSceneFinished;
		private SnsPostContext mPostContext;
		public bool doScene(SnsPostContext context)
		{
			this.mPostContext = context;
			base.beginBuilder();
			this.mBuilder.BaseRequest = NetSceneBase.makeBaseRequest(0);
			Util.ByteArrayToString(context.localSnsObject.bytesObjectDesc);
			this.mBuilder.ObjectDesc = Util.toSKBuffer(context.localSnsObject.bytesObjectDesc);
			if (context.localSnsObject.withList != null && context.localSnsObject.withList.list != null && context.localSnsObject.withList.list.get_Count() > 0)
			{
				this.mBuilder.WithUserListNum = (uint)context.localSnsObject.withList.list.get_Count();
				using (List<SnsComment>.Enumerator enumerator = context.localSnsObject.withList.list.GetEnumerator())
				{
					while (enumerator.MoveNext())
					{
						SnsComment current = enumerator.get_Current();
						this.mBuilder.WithUserListList.Add(Util.toSKString(current.strUserName));
					}
				}
			}
			this.mBuilder.Privacy = (uint)context.privacyFlag;
			this.mBuilder.SyncFlag = context.sync2WeiboFlag;
			if (context.mSnsTrans == null)
			{
				if (context.objectType != 2)
				{
					return false;
				}
				Log.smethod_5("NetSceneSnsPost", "context.objectType = " + context.objectType.ToString());
				this.mBuilder.ClientId = MD5Core.GetHashString(context.strContent + Util.getNowMilliseconds());
			}
			else
			{
				this.mBuilder.ClientId = context.mSnsTrans.strClientID;
			}
			this.mBuilder.PostBGImgType = (uint)context.bgImgType;
			if (context.localSnsObject.groupList.list != null && context.localSnsObject.groupList.list.get_Count() != 0)
			{
				this.mBuilder.GroupCount = (uint)context.localSnsObject.groupList.list.get_Count();
				using (List<ulong>.Enumerator enumerator2 = context.localSnsObject.groupList.list.GetEnumerator())
				{
					while (enumerator2.MoveNext())
					{
						ulong current2 = enumerator2.get_Current();
						this.mBuilder.GroupIdsList.Add(SnsGroup.CreateBuilder().SetGroupId(current2).Build());
					}
					goto IL_251;
				}
			}
			this.mBuilder.GroupCount = 0u;
			IL_251:
			this.mSessionPack.mCmdID = 97;
			base.endBuilder();
			context.mSnsObjectStatus = 2;
			return true;
		}
		public bool doScene(SnsInfo snsObj)
		{
			if (snsObj != null && snsObj.bytesObjectDesc != null)
			{
				base.beginBuilder();
				this.mBuilder.BaseRequest = NetSceneBase.makeBaseRequest(0);
				Util.ByteArrayToString(snsObj.bytesObjectDesc);
				this.mBuilder.ObjectDesc = Util.toSKBuffer(snsObj.bytesObjectDesc);
				if (snsObj.withList != null && snsObj.withList.list != null && snsObj.withList.list.get_Count() > 0)
				{
					this.mBuilder.WithUserListNum = (uint)snsObj.withList.list.get_Count();
					using (List<SnsComment>.Enumerator enumerator = snsObj.withList.list.GetEnumerator())
					{
						while (enumerator.MoveNext())
						{
							SnsComment current = enumerator.get_Current();
							this.mBuilder.WithUserListList.Add(Util.toSKString(current.strUserName));
						}
					}
				}
				this.mBuilder.Privacy = 0u;
				this.mBuilder.SyncFlag = 0u;
				this.mBuilder.ClientId = MD5Core.GetHashString(Util.getNowMilliseconds() + snsObj.bytesObjectDesc.ToString());
				this.mBuilder.PostBGImgType = 1u;
				if (snsObj.groupList.list != null && snsObj.groupList.list.get_Count() != 0)
				{
					this.mBuilder.GroupCount = (uint)snsObj.groupList.list.get_Count();
					using (List<ulong>.Enumerator enumerator2 = snsObj.groupList.list.GetEnumerator())
					{
						while (enumerator2.MoveNext())
						{
							ulong current2 = enumerator2.get_Current();
							this.mBuilder.GroupIdsList.Add(SnsGroup.CreateBuilder().SetGroupId(current2).Build());
						}
						goto IL_1CD;
					}
				}
				this.mBuilder.GroupCount = 0u;
				IL_1CD:
				this.mSessionPack.mCmdID = 97;
				base.endBuilder();
				return true;
			}
			return false;
		}
		protected override void onFailed(SnsPostRequest request, SnsPostResponse response)
		{
			Log.smethod_1("NetSceneSnsPost", "send request failed");
			RetConst retConst = RetConst.MM_ERR_CLIENT;
			if (this.mPostContext != null)
			{
				this.mPostContext.localSnsObject.nStatus = 4;
				StorageMgr.snsInfo.update(this.mPostContext.localSnsObject);
				this.mPostContext.mSnsObjectStatus = 4;
				this.mPostContext.mSnsTrans.nObjectstatus = this.mPostContext.mSnsObjectStatus;
				StorageMgr.snsTrans.update(this.mPostContext.mSnsTrans);
			}
			EventCenter.postEvent(EventConst.ON_NETSCENE_SNS_POST_ERR, retConst, null);
		}
		protected override void onSuccess(SnsPostRequest request, SnsPostResponse response)
		{
			RetConst ret = (RetConst)response.BaseResponse.Ret;
			if (ret != RetConst.MM_OK)
			{
				Log.smethod_1("NetSceneSnsPost", "send request failed ret =" + ret);
				if (this.mPostContext != null)
				{
					this.mPostContext.localSnsObject.nStatus = 4;
					StorageMgr.snsInfo.update(this.mPostContext.localSnsObject);
					this.mPostContext.mSnsObjectStatus = 4;
					this.mPostContext.mSnsTrans.nObjectstatus = this.mPostContext.mSnsObjectStatus;
					StorageMgr.snsTrans.update(this.mPostContext.mSnsTrans);
				}
				EventCenter.postEvent(EventConst.ON_NETSCENE_SNS_POST_ERR, ret, null);
				return;
			}
			if (this.mPostContext == null)
			{
				SnsInfo snsInfo = SnsInfoMgr.toSnsInfo(response.SnsObject);
				snsInfo.nStatus = 3;
				string text = Util.ByteArrayToString(snsInfo.bytesObjectDesc);
				Log.smethod_5("NetSceneSnsPost", "bytesObjectDesc = " + text);
				StorageMgr.snsInfo.update(snsInfo);
				EventCenter.postEvent(EventConst.ON_NETSCENE_SNS_POST_SUCCESS, ret, null);
				return;
			}
			string strObjectID = this.mPostContext.localSnsObject.strObjectID;
			SnsInfoMgr.releaseObjectID(strObjectID);
			SnsInfoMgr.addSentObjectID(response.SnsObject.Id);
			this.mPostContext.localSnsObject.nObjectID = response.SnsObject.Id;
			this.mPostContext.localSnsObject.nStatus = 3;
			if (StorageMgr.snsInfo.getByObjectID(SnsInfo.toStrID(this.mPostContext.localSnsObject.nObjectID)) != null)
			{
				StorageMgr.snsInfo.delByObjectID(SnsInfo.toStrID(this.mPostContext.localSnsObject.nObjectID));
			}
			StorageMgr.snsInfo.update(this.mPostContext.localSnsObject);
			Log.smethod_5("NetSceneSnsPost", "StorageMgr.snsInfo.update success objID = " + this.mPostContext.localSnsObject.nObjectID);
			if (this.mPostContext.objectType != 2)
			{
				this.mPostContext.mSnsObjectStatus = 5;
				this.mPostContext.mSnsTrans.nObjectstatus = this.mPostContext.mSnsObjectStatus;
			}
			StorageMgr.snsTrans.method_2(this.mPostContext.mSnsTrans);
			EventCenter.postEvent(EventConst.ON_NETSCENE_SNS_POST_SUCCESS, ret, null);
		}
	}
}
